//
//  HWGameCommunityDetailCommentCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/21.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameCommunityDetailCommentCell: UITableViewCell {
    var headImageView:UIImageView!
    var nameLabel:UILabel!
    var replyLabel:UILabel!
    var replyNameLabel:UILabel!
    var zanImageView:UIImageView!
    var zanLabel:UILabel!
    var zanButton:UIButton!
    var contentLabel:UILabel!
    var timeLabel:UILabel!
    var replyButton:UIButton!
    
    @objc var model:HWGameCommunityDetailCommentModel?{
        didSet {
            headImageView.kf.setImage(with: URL(string: model?.face ?? ""))
            nameLabel.text = model?.pet_name
            replyNameLabel.text = model?.p_pet_name
            contentLabel.text = model?.content
            timeLabel.text = model?.time
            zanLabel.text = model?.good
            if model?.is_good == true {
                zanImageView.image = UIImage(named: "communityZanSelect")
            } else {
                zanImageView.image = UIImage(named: "communityZanNormal")
            }
        }
    }
    
    @objc var replyButtonClick:((_:HWGameCommunityDetailCommentModel)->())?
    @objc var zanButtonClick:((_:HWGameCommunityDetailCommentModel)->())?
    
    @objc class func cellWithTableView(_ tableView:UITableView) -> (HWGameCommunityDetailCommentCell) {
        let reUse:String = "HWGameCommunityDetailCommentCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameCommunityDetailCommentCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameCommunityDetailCommentCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        headImageView = UIImageView()
        headImageView.layer.cornerRadius = 14*widthScale
        headImageView.layer.masksToBounds = true
        contentView.addSubview(headImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 13*widthScale)
        contentView.addSubview(nameLabel)
        
        replyLabel = HWLabel.createBoldLabel(title: "回复", color: "#888888", font: 13*widthScale)
        contentView.addSubview(replyLabel)
        
        replyNameLabel = HWLabel.createBoldLabel(title: "", color: "#333333", font: 13*widthScale)
        contentView.addSubview(replyNameLabel)
        
        zanImageView = UIImageView()
        zanImageView.image = UIImage(named: "communityZanNormal")
        contentView.addSubview(zanImageView)
        
        zanLabel = HWLabel.createLabel(title: "", color: "#888888", font: 13*widthScale)
        contentView.addSubview(zanLabel)
        
        zanButton = UIButton(type: .custom)
        zanButton.addTarget(self, action: #selector(zanButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(zanButton)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 13*widthScale)
        contentView.addSubview(contentLabel)
        
        timeLabel = HWLabel.createLabel(title: "", color: "#888888", font: 11*widthScale)
        contentView.addSubview(timeLabel)
        
        replyButton = UIButton(type: .custom)
        replyButton.setTitle("回复", for: .normal)
        replyButton.setTitleColor(.colorWithHexString(hexString: "#888888"), for: .normal)
        replyButton.titleLabel?.font = .systemFont(ofSize: 13*widthScale)
        replyButton.addTarget(self, action: #selector(replyButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(replyButton)
    }
    
    func initFrame() -> Void {
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(51*widthScale)
            make.top.equalTo(9*widthScale)
            make.width.height.equalTo(28*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(5*widthScale)
            make.centerY.equalTo(headImageView)
        }
        
        replyLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(5*widthScale)
            make.centerY.equalTo(nameLabel)
        }
        
        replyNameLabel.snp.makeConstraints { make in
            make.left.equalTo(replyLabel.snp.right).offset(5*widthScale)
            make.centerY.equalTo(replyLabel)
        }
        
        zanLabel.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.centerY.equalTo(zanImageView)
            make.height.equalTo(18*widthScale)
        }
        
        zanImageView.snp.makeConstraints { make in
            make.right.equalTo(zanLabel.snp.left).offset(-5*widthScale)
            make.width.height.equalTo(12*widthScale)
            make.top.equalTo(headImageView.snp.top).offset(3*widthScale)
        }
        
        zanButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(zanImageView)
            make.right.equalTo(zanLabel)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(51*widthScale)
            make.right.equalTo(-18*widthScale)
            make.top.equalTo(headImageView.snp.bottom).offset(9*widthScale)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(51*widthScale)
            make.top.equalTo(contentLabel.snp.bottom).offset(7*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
        
        replyButton.snp.makeConstraints { make in
            make.right.equalTo(-18*widthScale)
            make.centerY.equalTo(timeLabel)
        }
    }
    
    @objc func replyButtonClick(_:UIButton) -> Void {
        self.replyButtonClick?(model ?? HWGameCommunityDetailCommentModel())
    }
    
    @objc func zanButtonClick(_:UIButton) -> Void {
        self.zanButtonClick?(model ?? HWGameCommunityDetailCommentModel())
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
